﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;

public partial class Password_Recovery : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

    }
    protected void SubmitButton_Click(object sender, EventArgs e) {
        Decryption objdec = new Decryption();
        Label nameLabel = new Label();
        string subject = "Password Recovery";
        string password = "";
        string email = "";

        try {
            CreateConn objcon = new CreateConn();
            string query = "SELECT password, email, name FROM Users" +
                " WHERE username = '" + UserName.Text + "'";
            DataTable dt = objcon.SQLConnDataTable(query);

            password = dt.Rows[0]["password"].ToString();
            email = dt.Rows[0]["email"].ToString();
            nameLabel.Text = dt.Rows[0]["name"].ToString();

            string realPassword = objdec.DecryptString(password);
            string strMsg = "Your password is " + realPassword;

            if (password == "") {
                FailureText.Text = "unable to find username specified";
                FailureText.Visible = true;
            } else {
                Email objmail = new Email();
                if (!objmail.SendEmail(email, subject, strMsg)) {
                    FailureText.Text = "please try again...";
                    FailureText.Visible = true;
                } else {
                    FailureText.Text = "password has been sent!";
                    FailureText.Visible = true;
                    UserName.Text = String.Empty;
                }
            }
        } catch (Exception err) {
            Console.Write("Err msg:" + err.Message);
            FailureText.Text = "Sorry, we don't recognise the username specified...";
        }
    }
}
